<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of v_rotqr2ro</title>
  <meta name="keywords" content="v_rotqr2ro">
  <meta name="description" content="ROTQR2RO converts a real quaternion to a 3x3 rotation matrix">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">v_mfiles</a> &gt; v_rotqr2ro.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for v_mfiles&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>v_rotqr2ro
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>ROTQR2RO converts a real quaternion to a 3x3 rotation matrix</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function r=v_rotqr2ro(q) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment">ROTQR2RO converts a real quaternion to a 3x3 rotation matrix
 Inputs:

     Q(4,...)      Real-valued quaternion array (possibly unnormalized)

 Outputs:

     R(3,3,...)    Rotation matrix array
                   Plots a diagram if no output specified

 In the quaternion representation of a rotation, and q(1) = cos(t/2)
 where t is the angle of rotation in the range 0 to 2pi
 and q(2:4)/sin(t/2) is a unit vector lying along the axis of rotation
 a positive rotation about [0 0 1] takes the X axis towards the Y axis.

      Copyright (C) Mike Brookes 2007-2018
      Version: $Id: v_rotqr2ro.m 10865 2018-09-21 17:22:45Z dmb $

   VOICEBOX is a MATLAB toolbox for speech processing.
   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You can obtain a copy of the GNU General Public License from
   http://www.gnu.org/copyleft/gpl.html or by writing to
   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_rotqr2eu.html" class="code" title="function e=v_rotqr2eu(m,q)">v_rotqr2eu</a>	V_ROTQR2EQ converts a real unit quaternion into the corresponding euler angles</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_rectifyhomog.html" class="code" title="function [imr,xa,ya]=v_rectifyhomog(ims,roc,k0,mode)">v_rectifyhomog</a>	V_RECTIFYHOMOG Apply rectifying homographies to an image set</li><li><a href="v_rotax2qr.html" class="code" title="function q=v_rotax2qr(a,t)">v_rotax2qr</a>	V_ROTQR2AX converts a rotation axis and angle to the corresponding real quaternion</li><li><a href="v_roteu2qr.html" class="code" title="function q=v_roteu2qr(m,e)">v_roteu2qr</a>	ROTEU2QR converts a sequence of Euler angles to a real unit quaternion</li><li><a href="v_roteu2ro.html" class="code" title="function r=v_roteu2ro(varargin)">v_roteu2ro</a>	V_ROTEU2QR converts a sequence of Euler angles to a rotation matrix</li><li><a href="v_rotmc2qc.html" class="code" title="function qc=v_rotmc2qc(mc)">v_rotmc2qc</a>	V_ROTMC2QC converts a matrix of complex quaternion matrices to a matrix of complex quaternion vectors</li><li><a href="v_rotmr2qr.html" class="code" title="function qr=v_rotmr2qr(mr)">v_rotmr2qr</a>	V_ROTMR2QR converts a matrix of real quaternion matrices to quaternion vectors</li><li><a href="v_rotpl2ro.html" class="code" title="function r=v_rotpl2ro(u,v,t)">v_rotpl2ro</a>	V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]</li><li><a href="v_rotqc2mc.html" class="code" title="function mc=v_rotqc2mc(qc)">v_rotqc2mc</a>	V_ROTQC2MC converts a matrix of complex quaternion vectors to quaternion matrices</li><li><a href="v_rotqc2qr.html" class="code" title="function qr=v_rotqc2qr(qc)">v_rotqc2qr</a>	V_ROTQC2QR converts a matrix of complex quaternion row vectors into real form</li><li><a href="v_rotqr2ax.html" class="code" title="function [a,t]=v_rotqr2ax(q)">v_rotqr2ax</a>	V_ROTQR2AX converts a real quaternion to the corresponding rotation axis and angle</li><li><a href="v_rotqr2eu.html" class="code" title="function e=v_rotqr2eu(m,q)">v_rotqr2eu</a>	V_ROTQR2EQ converts a real unit quaternion into the corresponding euler angles</li><li><a href="v_rotqr2mr.html" class="code" title="function mr=v_rotqr2mr(qr)">v_rotqr2mr</a>	V_ROTQR2MR converts a matrix of real quaternion vectors to quaternion matrices</li><li><a href="v_rotqr2qc.html" class="code" title="function qc=v_rotqr2qc(qr)">v_rotqr2qc</a>	V_ROTQR2QC converts a matrix of real quaternion vectors into complex form</li><li><a href="v_rotqrvec.html" class="code" title="function y=v_rotqrvec(q,x)">v_rotqrvec</a>	V_ROTQRVEC applies a quaternion rotation ot a vector array y=[q,x]</li><li><a href="v_rotro2pl.html" class="code" title="function [u,v,t]=v_rotro2pl(r)">v_rotro2pl</a>	V_ROTRO2PL find the plane and rotation angle of a rotation matrix [u,v,t]=r</li><li><a href="v_rotro2qr.html" class="code" title="function q=v_rotro2qr(r)">v_rotro2qr</a>	V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function r=v_rotqr2ro(q)</a>
0002 <span class="comment">%ROTQR2RO converts a real quaternion to a 3x3 rotation matrix</span>
0003 <span class="comment">% Inputs:</span>
0004 <span class="comment">%</span>
0005 <span class="comment">%     Q(4,...)      Real-valued quaternion array (possibly unnormalized)</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% Outputs:</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%     R(3,3,...)    Rotation matrix array</span>
0010 <span class="comment">%                   Plots a diagram if no output specified</span>
0011 <span class="comment">%</span>
0012 <span class="comment">% In the quaternion representation of a rotation, and q(1) = cos(t/2)</span>
0013 <span class="comment">% where t is the angle of rotation in the range 0 to 2pi</span>
0014 <span class="comment">% and q(2:4)/sin(t/2) is a unit vector lying along the axis of rotation</span>
0015 <span class="comment">% a positive rotation about [0 0 1] takes the X axis towards the Y axis.</span>
0016 <span class="comment">%</span>
0017 <span class="comment">%      Copyright (C) Mike Brookes 2007-2018</span>
0018 <span class="comment">%      Version: $Id: v_rotqr2ro.m 10865 2018-09-21 17:22:45Z dmb $</span>
0019 <span class="comment">%</span>
0020 <span class="comment">%   VOICEBOX is a MATLAB toolbox for speech processing.</span>
0021 <span class="comment">%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html</span>
0022 <span class="comment">%</span>
0023 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0024 <span class="comment">%   This program is free software; you can redistribute it and/or modify</span>
0025 <span class="comment">%   it under the terms of the GNU General Public License as published by</span>
0026 <span class="comment">%   the Free Software Foundation; either version 2 of the License, or</span>
0027 <span class="comment">%   (at your option) any later version.</span>
0028 <span class="comment">%</span>
0029 <span class="comment">%   This program is distributed in the hope that it will be useful,</span>
0030 <span class="comment">%   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
0031 <span class="comment">%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
0032 <span class="comment">%   GNU General Public License for more details.</span>
0033 <span class="comment">%</span>
0034 <span class="comment">%   You can obtain a copy of the GNU General Public License from</span>
0035 <span class="comment">%   http://www.gnu.org/copyleft/gpl.html or by writing to</span>
0036 <span class="comment">%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.</span>
0037 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0038 
0039 <span class="keyword">persistent</span> a b c d e f g h m
0040 <span class="keyword">if</span> isempty(a)
0041     a=[1 5 9];
0042     b=[11 16 6];
0043     c=[16 6 11];
0044     d=[4 8 3];
0045     e=[10 15 14];
0046     f=[4 2 3];
0047     g=[2 6 7];
0048     h=[1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4]';
0049     m=[1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4]';
0050 <span class="keyword">end</span>
0051 sz=size(q);
0052 q=reshape(q,4,[]);      <span class="comment">% convert to 2D matrix</span>
0053 nq =size(q,2);          <span class="comment">% number of quaternions to convert</span>
0054 p=2*q(h,:).*q(m,:)./repmat(sum(q.^2,1),16,1); <span class="comment">% force normalized and calculate quadratic terms</span>
0055 r=zeros(9,nq);          <span class="comment">% space for nq rotation matrices</span>
0056 r(a,:)=1-p(b,:)-p(c,:);
0057 r(d,:)=p(e,:)-p(f,:);
0058 r(g,:)=p(e,:)+p(f,:);
0059 r=reshape(r,[3 3 sz(2:end)]);
0060 <span class="keyword">if</span> ~nargout
0061     <span class="comment">% display rotated cube</span>
0062     q1=q(:,1);
0063     q1=q1/sqrt(q1'*q1); <span class="comment">% normalize the first quaternion</span>
0064     clf(<span class="string">'reset'</span>); <span class="comment">% clear current axis</span>
0065     v0=[-1 1 1 -1 -1 1 1 -1; -1 -1 1 1 -1 -1 1 1; -1 -1 -1 -1 1 1 1 1]*0.5; <span class="comment">% unrotated coordinates</span>
0066     v=r(:,:,1)*v0; <span class="comment">% use first elemnt of r to rotate</span>
0067     fv=[4 1 5 8; 2 3 7 6; 1 2 6 5; 3 4 8 7; 2 1 4 3; 5 6 7 8]; <span class="comment">% verices for each face</span>
0068     fc=[0 1 1; 1 0 0; 1 0 1; 0 1 0; 1 1 0; 0 0 1]; <span class="comment">% colours for faces</span>
0069     xc={[25 46 46 25; 55 55 49 49]/100,
0070         [25 33 33 39 39 46 46 39 39 33 33 25; 55 55 63 63 55 55 49 49 42 42 49 49]/100,
0071         [48 56 62 69 76 66 76 69 62 56 48 59; 68 68 58 68 68 53 37 37 47 37 37 53]/100,
0072         [48 55 62 69 77 65 65 59 59; 68 68 55 68 68 50 37 37 50]/100,
0073         [50 74 74 57 74 74 50 50 67 50; 68 68 62 43 43 37 37 43 62 62]/100};
0074     xf=[1 3; 2 3; 1 4; 2 4; 1 5; 2 5]; <span class="comment">% characters to plot on each face</span>
0075     nf=size(fv,1); <span class="comment">% number of faces</span>
0076     <span class="keyword">for</span> i=1:6
0077         p(i)=patch(v(1,fv(i,:)),v(2,fv(i,:)),v(3,fv(i,:)),fc(i,:));
0078         set(p(i),<span class="string">'FaceAlpha'</span>,0.65);
0079         k=1.001; <span class="comment">% factor to move out labels slightly to get correct depth ordering</span>
0080         <span class="keyword">for</span> j=1:2
0081             xij=xc{xf(i,j)}; <span class="comment">% relative coordinates of character vertices</span>
0082             patch(k*(v(1,fv(i,1))+(v(1,fv(i,2))-v(1,fv(i,1)))*xij(1,:)+(v(1,fv(i,4))-v(1,fv(i,1)))*xij(2,:)), <span class="keyword">...</span>
0083                 k*(v(2,fv(i,1))+(v(2,fv(i,2))-v(2,fv(i,1)))*xij(1,:)+(v(2,fv(i,4))-v(2,fv(i,1)))*xij(2,:)), <span class="keyword">...</span>
0084                 k*(v(3,fv(i,1))+(v(3,fv(i,2))-v(3,fv(i,1)))*xij(1,:)+(v(3,fv(i,4))-v(3,fv(i,1)))*xij(2,:)),1-fc(i,:));
0085         <span class="keyword">end</span>
0086     <span class="keyword">end</span>
0087     qa=q1(2:4);
0088     qm=max(abs(qa));
0089     <span class="keyword">if</span> qm&gt;1e-6*abs(q1(1))
0090         qa=qa/(qm/0.7); <span class="comment">% scale so axis extends outside the cube</span>
0091         hold on;
0092         plot3([1 -1]*qa(1),[1 -1]*qa(2),[1 -1]*qa(3),<span class="string">'-'</span>);
0093         hold off
0094     <span class="keyword">end</span>
0095     th=360/pi*acos(abs(q1(1)));
0096     xlabel(<span class="string">'x axis'</span>);
0097     ylabel(<span class="string">'y axis'</span>);
0098     zlabel(<span class="string">'z axis'</span>);
0099     q=q(:,1)*((2*(q(find(q1(:)~=0,1))&gt;0)-1)); <span class="comment">% force leading coefficient to be positive</span>
0100     title(sprintf(<span class="string">'%d^\\circ, qr'' = [%.2f,%.2f,%.2f,%.2f], eu_{xyzo}'' = [%d, %d, %d]^\\circ'</span>,round(th),q1(:),round(<a href="v_rotqr2eu.html" class="code" title="function e=v_rotqr2eu(m,q)">v_rotqr2eu</a>(<span class="string">'xyz'</span>,q1)*180/pi)));
0101     axis([-1 1 -1 1 -1 1 0 1]*sqrt(3)/2);
0102     axis equal
0103     grid on
0104     view(3);
0105 <span class="keyword">end</span>
0106</pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>